package com.ph.irp.base.verifyproject.action;

import java.util.List;
import com.opensymphony.xwork2.ActionSupport;
import com.ph.irp.base.verifyproject.service.PBaseVerifyprojectDepartmentService;
import com.ph.irp.dto.DepartmentDTO;
import com.ph.irp.model.PBaseVerifyprojectDepartment;

public class VerifyprojectDepartmentAction extends ActionSupport {
	/**
	 * 所用到的Service接口
	 */
	private PBaseVerifyprojectDepartmentService departmentService;

	public void setDepartmentService(
			PBaseVerifyprojectDepartmentService departmentService) {
		this.departmentService = departmentService;
	}

	private PBaseVerifyprojectDepartment verifyprojectDepartment;

	private DepartmentDTO depart;

	private List<DepartmentDTO> departments;

	public PBaseVerifyprojectDepartment getVerifyprojectDepartment() {
		return verifyprojectDepartment;
	}

	public void setVerifyprojectDepartment(
			PBaseVerifyprojectDepartment verifyprojectDepartment) {
		this.verifyprojectDepartment = verifyprojectDepartment;
	}

	public DepartmentDTO getDepart() {
		return depart;
	}

	public void setDepart(DepartmentDTO depart) {
		this.depart = depart;
	}

	public List<DepartmentDTO> getDepartments() {
		return departments;
	}

	public void setDepartments(List<DepartmentDTO> departments) {
		this.departments = departments;
	}

	private Object[] rowlist = null;

	public Object[] getRowlist() {
		return rowlist;
	}

	public void setRowlist(Object[] rowlist) {
		this.rowlist = rowlist;
	}

	public String showDepartmentList() throws Throwable {
		verifyprojectDepartment = null;
		departments = departmentService.getAllDepartment();
		return "success";
	}

	public String searchDepartment() throws Throwable {
		if (verifyprojectDepartment == null) {
			departments = departmentService.getAllDepartment();
		} else {
			if (!verifyprojectDepartment.getDepartmentCode().equals("")
					&& !verifyprojectDepartment.getDepartmentName().equals("")) {
				departments = departmentService.getDepartmentByCodeAndName(
						verifyprojectDepartment.getDepartmentCode(),
						verifyprojectDepartment.getDepartmentName());
			} else if (!verifyprojectDepartment.getDepartmentCode().equals("")) {
				departments = departmentService
						.getDepartmentByCode(verifyprojectDepartment
								.getDepartmentCode());
			} else if (!verifyprojectDepartment.getDepartmentName().equals("")) {
				departments = departmentService
						.getDepartmentByName(verifyprojectDepartment
								.getDepartmentName());
			} else {
				departments = departmentService.getAllDepartment();
			}
		}
		verifyprojectDepartment = null;
		return "success";
	}

	public String addInputDepartment() {

		depart = new DepartmentDTO();
		return "success";
	}

	public String addDepartment() throws Throwable {
		departmentService.addDepartment(depart.getDepartment());
		return "success";
	}

	public String updateDepartment() throws Throwable {
		departmentService.updateDepartment(depart.getDepartment());
		return "success";
	}

	public String updateInputDepartment() throws Throwable {
		Integer departmentId = Integer.valueOf((String) rowlist[0]);
		depart = departmentService.getDepartmentById(departmentId);
		return "success";
	}

	public String deleteDepartment() throws Throwable {
		for (int i = 0; i < rowlist.length; i++) {
			departmentService.delateDepartment(Integer
					.valueOf((String) rowlist[i]));
		}
		return "success";
	}

	public String selectDepartment() throws Throwable {
		if (verifyprojectDepartment == null) {
			departments = departmentService.getAllDepartment();
		} else {
			if (!verifyprojectDepartment.getDepartmentCode().equals("")
					&& !verifyprojectDepartment.getDepartmentName().equals("")) {
				departments = departmentService.getDepartmentByCodeAndName(
						verifyprojectDepartment.getDepartmentCode(),
						verifyprojectDepartment.getDepartmentName());
			} else if (!verifyprojectDepartment.getDepartmentCode().equals("")) {
				departments = departmentService
						.getDepartmentByCode(verifyprojectDepartment
								.getDepartmentCode());
			} else if (!verifyprojectDepartment.getDepartmentName().equals("")) {
				departments = departmentService
						.getDepartmentByName(verifyprojectDepartment
								.getDepartmentName());
			} else {
				departments = departmentService.getAllDepartment();
			}
		}
		return "success";
	}
}
